#include "scheduler/retry_scheduler.h"
#include "../policy/behavior_tree/behavior_tree_retry_policy.h"
#include "../policy/policy_factory.h"

namespace scheduler
{
RetryScheduler::RetryScheduler(const std::string &name)
    : AbstractScheduler(name)
{
    schedule_policy_ = policy::PolicyFactory<policy::BehaviorTreeRetryPolicy>::createPolicy(name, 1);
}
RetryScheduler::RetryScheduler(const std::string &name, int retry_count)
    : AbstractScheduler(name)
{
    schedule_policy_ = policy::PolicyFactory<policy::BehaviorTreeRetryPolicy>::createPolicy(name, retry_count);
}
RetryScheduler::~RetryScheduler() = default;

bool RetryScheduler::appendTask(const task::AbstractTaskConstPtr &task)
{
    EASE_ERROR("retry scheduler cannot add task");
    throw std::runtime_error("retry scheduler cannot add task");
}
bool RetryScheduler::appendScheduler(const AbstractSchedulerConstPtr &scheduler)
{
    EASE_ERROR("retry scheduler cannot add scheduler");
    throw std::runtime_error("retry scheduler cannot add scheduler");
}

void RetryScheduler::setRetryCount(int32_t count)
{
    auto *policy = dynamic_cast<policy::BehaviorTreeRetryPolicy *>(schedule_policy_.get());
    if (nullptr == policy)
    {
        throw std::runtime_error("retry scheduler case fail");
    }
    policy->setRetryCount(count);
}
int32_t RetryScheduler::getRetryCount() const
{
    auto *policy = dynamic_cast<policy::BehaviorTreeRetryPolicy *>(schedule_policy_.get());
    if (nullptr == policy)
    {
        throw std::runtime_error("retry scheduler case fail");
    }
    return policy->getRetryCount();
}

}
